package cn.ch.train.business.service;

import cn.ch.train.business.domain.DailyTrainCarriage;
import cn.ch.train.business.domain.DailyTrainCarriageExample;
import cn.ch.train.business.enums.SeatColEnum;
import cn.ch.train.business.mapper.DailyTrainCarriageMapper;
import cn.ch.train.business.request.DailyTrainCarriageQueryParam;
import cn.ch.train.business.request.DailyTrainCarriageSaveParam;
import cn.ch.train.business.response.DailyTrainCarriageQueryResult;
import cn.ch.train.common.response.PageResult;
import cn.ch.train.common.util.SnowUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class DailyTrainCarriageService {
    private static final Logger logger = LoggerFactory.getLogger(DailyTrainCarriageService.class);
    @Resource
    private DailyTrainCarriageMapper dailyTrainCarriageMapper;

    public void save(DailyTrainCarriageSaveParam param) {
        DateTime now = DateTime.now();
        // 自动计算出列数和总座位数
        List<SeatColEnum> seatColEnumList = SeatColEnum.getColsByType(param.getSeatType());
        param.setColCount(seatColEnumList.size());
        param.setSeatCount(param.getColCount() * param.getRowCount());
        DailyTrainCarriage dailyTrainCarriage = BeanUtil.copyProperties(param, DailyTrainCarriage.class);
        if (Objects.isNull(dailyTrainCarriage.getId())) {
            dailyTrainCarriage.setId(SnowUtil.getSnowFlakeNextId());
            dailyTrainCarriage.setCreateTime(now);
            dailyTrainCarriage.setUpdateTime(now);
            dailyTrainCarriageMapper.insert(dailyTrainCarriage);
        } else {
            dailyTrainCarriage.setUpdateTime(now);
            dailyTrainCarriageMapper.updateByPrimaryKey(dailyTrainCarriage);
        }
    }

    public PageResult<DailyTrainCarriageQueryResult> queryList(DailyTrainCarriageQueryParam param) {
        DailyTrainCarriageExample dailyTrainCarriageExample = new DailyTrainCarriageExample();
        dailyTrainCarriageExample.setOrderByClause("date asc, train_code asc");
        DailyTrainCarriageExample.Criteria criteria = dailyTrainCarriageExample.createCriteria();
        if (ObjectUtil.isNotNull(param.getDate())) {
            criteria.andDateEqualTo(param.getDate());
        }
        if (ObjectUtil.isNotEmpty(param.getTrainCode())) {
            criteria.andTrainCodeEqualTo(param.getTrainCode());
        }
        PageHelper.startPage(param.getPageNo(), param.getPageSize());
        List<DailyTrainCarriage> dailyTrainCarriageList = dailyTrainCarriageMapper.selectByExample(dailyTrainCarriageExample);

        PageInfo<DailyTrainCarriage> dailyTrainCarriagePageInfo = new PageInfo<>(dailyTrainCarriageList);
        logger.info("总行数：{}", dailyTrainCarriagePageInfo.getTotal());
        logger.info("总页数：{}", dailyTrainCarriagePageInfo.getPages());
        List<DailyTrainCarriageQueryResult> dailyTrainCarriageQueryList = BeanUtil.copyToList(dailyTrainCarriageList, DailyTrainCarriageQueryResult.class);
        PageResult<DailyTrainCarriageQueryResult> pageResult = new PageResult<>();
        pageResult.setTotal(dailyTrainCarriagePageInfo.getTotal());
        pageResult.setList(dailyTrainCarriageQueryList);
        return pageResult;
    }

    public void delete(Long id) {
        dailyTrainCarriageMapper.deleteByPrimaryKey(id);
    }
}
